home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / SITES / RAND / DVIEW000.LZH / TRIG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-09  |  3.0 KB  |  132 lines

  1. /********************************************************************
  2.  FILENAME: TRIG.CPP
  3.  AUTHOR  : JAKE HILL
  4.  DATE    : 12/1/94
  5.  
  6.  Copyright (c) 1994 by Jake Hill:
  7.  If you use any part of this code in your own project, please credit
  8.  me in your documentation and source code.  Thanks.
  9. ********************************************************************/
  10. #include "trig.h"
  11.  
  12. #include <math.h>
  13.  
  14. float pi = 3.141592654;
  15.  
  16. /* These are the lookup tables. */
  17. long sin_table[1024];
  18. long tan_table[1024];
  19. long cos_table[1024];
  20. long inv_cos_table[1024];
  21. long inv_dist_table[10000];
  22.  
  23.  
  24. /* returns x * sin(a) */
  25.  
  26. short xSinA(short x, unsigned short a)
  27. {
  28.    long tx = x;
  29.    return (short)((tx * sine(a)) >> 16);
  30. }
  31.  
  32.  
  33. /* returns x * cos(a) */
  34.  
  35. short xCosA(short x, unsigned short a)
  36. {
  37.    long tx = x;
  38.    return (short)((tx * cosine(a)) >> 16);
  39. }
  40.  
  41.  
  42. #if 0
  43. /* returns sin(angle)
  44.  * This function is only used during the trig initialization process.
  45.  */
  46.  
  47. long Sine(long angle)
  48. {
  49.    double radians = (angle * pi) / 32768L;
  50.    return  (long)(sin(radians) * 65536L);
  51. }
  52.  
  53. /* returns cos(angle)
  54.  * This function is only used during the trig initialization process.
  55.  */
  56. long Cosine(long angle)
  57. {
  58.    double radians = (angle * pi) / 32768L;
  59.    return (long)(cos(radians) * 65536L);
  60. }
  61.  
  62.  
  63. /* returns tan(angle)
  64.  * This function is only used during the trig initialization process.
  65.  */
  66.  
  67. long Tangent(long angle)
  68. {
  69.    double radians = (angle * pi) / 32768L;
  70.    return (long)(tan(radians) * 65536L);
  71. }
  72.  
  73.  
  74. /* returns 1/(cos(angle))
  75.  * This function is only used during the trig initialization process.
  76.  */
  77. long InvCosine(long angle)
  78. {
  79.    double radians = (angle * pi) / 32768L;
  80.  
  81. /*   if (cos(radians) == 0) */
  82.         return 0;
  83.    if (cos(radians) == 0)
  84.       return 65536L;
  85.    return (long)(65536L / cos(radians));
  86. }
  87. #endif
  88.  
  89.  
  90. /* Generate a lookup table for distances based on width of screen.
  91.  * These values are used in the perspective calculations in order
  92.  * to avoid doing a 32 bit division during the rendering process.
  93.  * This function should be used whenever the screen width is changed.
  94.  */
  95. void GenInvDistTable(long screen_width)
  96. {
  97.    long z;
  98.    long numerator = (screen_width / 2) * 65536L;
  99.  
  100.    for(z = 0;z < 10000;z++)
  101.       inv_dist_table[z] = numerator / (z + 1);
  102. }
  103.  
  104. /* Initialize all of the lookup tables for use in the renderer. */
  105. void InitTrig(void)
  106. {
  107.    double radians, s, c;
  108.    long i;
  109.  
  110.    GenInvDistTable(320);
  111.  
  112.    for(i = 0;i < 1024L;i++) {
  113.       radians = ((i * 64L) * pi) / 32768L;
  114.       s = sin(radians);
  115.       c = cos(radians);
  116.  
  117. #if 0
  118.       sin_table[i] = Sine(i * 64L);
  119.       cos_table[i] = Cosine(i * 64L);
  120.       tan_table[i] = Tangent(i * 64L);
  121.       inv_cos_table[i] = InvCosine(i * 64L);
  122. #endif
  123.       sin_table[i] = (long)(s * 65536L);
  124.       cos_table[i] = (long)(c * 65536L);
  125.       tan_table[i] = (long)((s / c) * 65536L);
  126.       if (c == 0)
  127.          inv_cos_table[i] = 65536L;
  128.       else
  129.          inv_cos_table[i] = (long)(65536L / c);
  130.    }
  131. }
  132.